<?php
/**
 * 功能：后台菜单控制器
 * 作者：魏安来
 * 日期：2017/01/16
 */

namespace app\index\controller;
use app\index\controller\Menu;
use app\index\model\AuthGroup;
use app\index\model\AuthRule;

class AuthRole extends Base {

	/*角色列表*/
	public function index() {
		//调用左侧菜单
		$menuController = new Menu();
		$menuController->sidebar();
		//查询所有角色
		$roleModel = new AuthGroup();
		$allRole = $roleModel->getAllRole();
		$this->assign('allRole', $allRole);
		return $this->fetch();
	}

	/*添加角色*/
	public function add() {
		$authRuleModel = new AuthRule();
		/*获取权限规则数据整理成树形结构*/
		$authRuleList = getTree($authRuleModel->getAllRule());
		/*将树形结构整理成树形结构HTML输出*/
		$html = htmlTree($authRuleList);
		$this->assign('html',$html);
		if (request()->isAjax()) {
			//接收数据
			$data = input('post.');
			if (!isset($data['status'])) {
				$data['status'] = 0;
			} else {
				$data['status'] = 1;
			}
			if (isset($data['rules'])) {
				$rules = $data['rules'];
				$data['rules'] = implode(',', $rules);
			}
			//定义验证规则
            $rule = [
				'title' => 'require|min:2|unique:auth_rule,title='.$data['title'],
            ];

            //定义错误提示信息
            $message = [
                'title.require' => '角色名称不能为空',
				'title.min' => '角色名称不能少于2个字符',
				'title.unique' => '角色名称已存在'
            ];
			$validate = new \think\Validate($rule, $message);
            $result = $validate->check($data);
			if ($result) {
				//写入数据库
				$affected = db('auth_group')->insert($data);
				if ($affected) {
					return 'succ';
				} else {
					return 'error';
				}
			} else {
				return $validate->getError();
			}
		}
		return $this->fetch();
	}

	/*角色编辑*/
	public function edit() {
		$authRuleModel = new AuthRule();
		$roleModel = new AuthGroup();
		//接收要编辑的权限规则id
		$id = request()->param('id');
		//查找上级权限规则
		$this->assign('id', $id);
		/*获取需要编辑的权限规则数据*/
		$oneRoleData = $roleModel->getOneRoleData($id);
		$rules = explode(',', $oneRoleData['rules']);
		$this->assign('oneRoleData',$oneRoleData);
		/*获取权限规则数据整理成树形结构*/
		$authRuleList = getTree($authRuleModel->getAllRule());
		/*将树形结构整理成树形结构HTML输出*/
		$html = htmlTree($authRuleList, $rules);
		$this->assign('html',$html);
		if (request()->isAjax()) {
			$data = input('post.');
			$id = $data['id'];
			unset($data['id']);
			//处理是否显示字段
			if (!isset($data['status'])) {
				$data['status'] = 0;
			} else {
				$data['status'] = 1;
			}
			if (isset($data['rules'])) {
				$rules = $data['rules'];
				$data['rules'] = implode(',', $rules);
			} else {
				$data['rules'] = '';
			}
			//如果没有修改角色名称就不验证角色名称是否已存在
			if (trim($data['title']) != trim($oneRoleData['title'])) {
				//定义验证规则
	            $rule = [
					'title' => 'require|min:2|unique:auth_group,title='.$data['title'], 
	            ];

	            //定义错误提示信息
	            $message = [
	                'title.require' => '角色名称不能为空',
					'title.min' => '角色名称不能少于2个字符',
					'title.unique' => '角色名称已存在',
	            ];
	            $validate = new \think\Validate($rule, $message);
            	$result = $validate->check($data);
			} else {
				$result = 'true';
			}
			//print_r($data);
			if ($result){
				$roleModel = new AuthGroup();
				$affected = $roleModel->updataOneRoleData($id, $data);
				if ($affected) {
					return 'succ';
				} else {
					return 'error';
				}
			} else {
				return $validate->getError();
			}
		}
		return $this->fetch();
	}

	/*删除角色*/
	public function delete() {
		$id = request()->param('id');
		$roleModel = new AuthGroup();
		$affected = $roleModel->deleteOneRole($id);
		if ($affected) {
			return 'succ';
		} else {
			return 'error';
		}
	}

}

?>